package com.example.questionsystem.modules.qu.service.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class QuestionExtractor {

    // 小题对象
    public static class SubQuestion {
        private String type;   // 小题类型，例如【简答题】【填空题】
        private String html;   // 小题HTML内容

        public SubQuestion(String type, String html) {
            this.type = type;
            this.html = html;
        }

        public String getType() {
            return type;
        }

        public String getHtml() {
            return html;
        }

        @Override
        public String toString() {
            return "SubQuestion{" +
                    "type='" + type + '\'' +
                    ", html='" + html + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws Exception {
        // 读取文件
        // 读取文件
        File input = new File("C:\\Users\\Administrator\\Desktop\\34.txt");
        Document doc = Jsoup.parse(input, StandardCharsets.UTF_8.name());


        boolean inStem = false;
        boolean inSubQ = false;

        StringBuilder stemBuilder = new StringBuilder();
        List<SubQuestion> subQuestions = new ArrayList<>();

        String currentType = null;
        StringBuilder subQBuilder = new StringBuilder();

        for (Element element : doc.body().children()) {
            String text = element.outerHtml();       // 保留完整 HTML（包括 <table>）
            String plain = element.text().trim();    // 提取纯文本，用来判断标识

            // 题干开始
            if (plain.contains("【题干】")) {
                inStem = true;
                String cleaned = text.replace("【题干】", "");
                stemBuilder.append(cleaned);
                continue;
            }

            // 题目结束
            if (plain.contains("【题目结束】")) {
                if (inSubQ && currentType != null) {
                    subQuestions.add(new SubQuestion(currentType, subQBuilder.toString()));
                }
                break;
            }

            // 小题开始
            if (plain.matches(".*【.*题】.*")) {
                if (inSubQ && currentType != null) {
                    subQuestions.add(new SubQuestion(currentType, subQBuilder.toString()));
                    subQBuilder.setLength(0);
                }
                inStem = false;
                inSubQ = true;

                // 提取题型
                String type = plain.replaceAll(".*(【.*题】).*", "$1");
                currentType = type.replace("【", "").replace("】", "");

                // ⚡ 不仅清理当前标识，还要忽略它本身，把内容交给后续节点
                String cleaned = text.replace(type, "").trim();
                if (!cleaned.isEmpty() && !cleaned.equals("<p></p>")) {
                    subQBuilder.append(cleaned);
                }
                continue;
            }

            // 内容保存（包括 table）
            if (inStem) {
                stemBuilder.append(text);
            } else if (inSubQ) {
                subQBuilder.append(text);
            }
        }

        System.out.println("题干HTML：");
        System.out.println(stemBuilder);

        System.out.println("\n小题：");
        for (SubQuestion sq : subQuestions) {
            System.out.println("题型: " + sq.getType());
            System.out.println("内容: " + sq.getHtml());
            System.out.println("---------------------------------------------------");
        }
    }
}
